package cc.mcggdd.leah.boot.stock.controller;

import cc.mcggdd.leah.boot.stock.model.query.StockTransferStatisticQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockTransferStatisticVO;
import cc.mcggdd.leah.boot.stock.service.StockTransferService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cc.mcggdd.leah.boot.stock.model.form.StockTransferForm;
import cc.mcggdd.leah.boot.stock.model.query.StockTransferQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockTransferVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cc.mcggdd.leah.boot.common.result.PageResult;
import cc.mcggdd.leah.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.math.BigDecimal;
import java.util.List;

/**
 * 银证转账前端控制层
 *
 * @author zifeng
 * @since 2025-02-07 10:03
 */
@Tag(name = "银证转账接口")
@RestController
@RequestMapping("/api/v1/stockTransfers")
@RequiredArgsConstructor
public class StockTransferController  {

    private final StockTransferService stockTransferService;

    @Operation(summary = "银证转账分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:query')")
    public PageResult<StockTransferVO> getStockTransferPage(StockTransferQuery queryParams ) {
        IPage<StockTransferVO> result = stockTransferService.getStockTransferPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "银证转账统计")
    @GetMapping("/statistic")
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:query')")
    public Result<StockTransferStatisticVO> getStockTransferStatistic(StockTransferStatisticQuery queryStatisticParams ) {
        StockTransferStatisticVO result = stockTransferService.getStockTransferStatistic(queryStatisticParams);
        if (null == result) {
            result = new StockTransferStatisticVO();
            result.setStartDate(String.valueOf(queryStatisticParams.getTranDate().get(0)));
            result.setEndDate(String.valueOf(queryStatisticParams.getTranDate().get(1)));
            result.setTotalAmount(new BigDecimal(0));
        }else {
            result.setMonthItemVOList(stockTransferService.getStockTransferMonthRollup(queryStatisticParams));
        }
        return Result.success(result);
    }

    @Operation(summary = "新增银证转账")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:add')")
    public Result<Void> saveStockTransfer(@RequestBody @Valid StockTransferForm formData ) {
        boolean result = stockTransferService.saveStockTransfer(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取银证转账表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:edit')")
    public Result<StockTransferForm> getStockTransferForm(
        @Parameter(description = "银证转账ID") @PathVariable Long id
    ) {
        StockTransferForm formData = stockTransferService.getStockTransferFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改银证转账")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:edit')")
    public Result<Void> updateStockTransfer(
            @Parameter(description = "银证转账ID") @PathVariable Long id,
            @RequestBody @Validated StockTransferForm formData
    ) {
        boolean result = stockTransferService.updateStockTransfer(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除银证转账")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('stock:stockTransfer:delete')")
    public Result<Void> deleteStockTransfers(
        @Parameter(description = "银证转账ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = stockTransferService.deleteStockTransfers(ids);
        return Result.judge(result);
    }
}
